package com.zhangyang;

import java.nio.ByteBuffer;

/**
 * Created by ASUS on 2017/10/13.
 */
public class BufferSlice
{
    public static void main(String []args){
        //初始化缓冲区
        ByteBuffer byteBuffer=ByteBuffer.allocate(10);
        System.out.println("position:"+byteBuffer.position()+"...limit:"+byteBuffer.limit());
        //初始化数据
        for(int i=0;i<byteBuffer.capacity();++i){
            byteBuffer.put((byte) i);
        }

        //定位到对应的位置
        byteBuffer.position(3);
        byteBuffer.limit(7);

        ByteBuffer sliceByteBuffer=byteBuffer.slice();
        //对子缓冲区重新设置值 对应到原先的缓冲区 值
        for (int i = 0; i < sliceByteBuffer.capacity(); ++i) {
            sliceByteBuffer.put((byte) (sliceByteBuffer.get(i)*10));
        }

        //重新设置位置
        byteBuffer.position(0);
        byteBuffer.limit(byteBuffer.capacity());
        while (byteBuffer.hasRemaining()){
            System.out.print("  "+byteBuffer.get());
        }
    }
}
